package com.wash.shoes.util;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * @author lijianguo
 * @Date 28/09/2024
 * 阿里云短信发送
 **/
@Log4j2
@Component
public class AliYunSmsUtil {

    @Resource
    Client client;

    private static Client clientStatic;

    @PostConstruct
    void init() {
        clientStatic = client;
//        sendShoeSms("李文中","18382496398","18382496398");

    }

    /**
     * @author lijianguo
     * @Date 28/09/2024
     * 发送短信
     **/
    public static String sendSms(String templateCode, String kdName, String kdPhone, String userPhone) {
        // 不影响主要的业务
        try {
            //
            String[] kdPhoneNum = kdPhone.split("或");

            // 您的洗鞋订单已安排揽收，可联系快递员：${name}，电话：${phone}
            JSONObject param = new JSONObject();
            param.put("name", kdName);
            param.put("phone", kdPhoneNum[0]);


            SendSmsRequest sendSmsRequest = new SendSmsRequest().setPhoneNumbers(userPhone).setSignName("梵尘").setTemplateCode(templateCode).setTemplateParam(param.toJSONString());
            SendSmsResponse sendSmsResponse = clientStatic.sendSms(sendSmsRequest);
            log.info("user info {} {} {}", kdName, kdPhone, userPhone);
            log.info("send sms is {}", JSONObject.toJSONString(sendSmsResponse));
        } catch (Exception e) {
            log.error(e);
        }
        return "";
    }


    /**
     * @author lijianguo
     * @Date 21/10/2024
     * 店铺获取鞋子
     **/
    public static String receiveShoeSms(String kdName, String kdPhone, String userPhone) {
        return sendSms("SMS_484710175", kdName, kdPhone, userPhone);
    }

    /**
     * @author lijianguo
     * @Date 21/10/2024
     * 寄送获取鞋子
     **/
    public static String sendShoeSms(String kdName, String kdPhone, String userPhone) {
        return sendSms("SMS_484720187", kdName, kdPhone, userPhone);
    }

}
